import request from './request'
import { useUserStore } from '../store/modules/user'
import { AxiosPromise } from 'axios'
// 登录
export function login(data: any) {
  return request({
    url: '/system/login/',
    method: 'post',
    data,
  })
}
// 退出登录
export function loginOut() {
  let token = {
    'access-token': useUserStore().row.tokenValue,
  }
  return request({
    url: '/system/login/logout',
    method: 'get',
    headers: token,
  })
}
// 短信登录验证码
export function loginCode(params: { phone: string }) {
  return request({
    url: '/sms/loginCode',
    method: 'get',
    params,
  })
}
// 短信注册验证码
export function registerCode(params: { phone: string }) {
  return request({
    url: '/sms/registerCode',
    method: 'get',
    params,
  })
}
// 更新验证码
export function updateCode(params: { phone: string }) {
  return request({
    url: '/sms/updateCode',
    method: 'get',
    params,
  })
}
// 未登录更新
export function notLoginUpdatePassword(data: {
  phone: string
  newPassword: string
  oldPassword: string
  type: number
}) {
  return request({
    url: '/system/account/notLoginUpdatePassword',
    method: 'PUT',
    data,
  })
}
// 注册
export function register(data: any) {
  return request({
    url: '/system/account/register',
    method: 'post',
    data,
  })
}
// 获取账号关联所有企业
export function getCustomer() {
  let token = {
    'access-token': useUserStore().row.tokenValue,
  }
  return request({
    url: '/system/login/getCustomer',
    method: 'get',
    headers: token,
  })
}
// 选择企业
export function getCustomerCode(params) {
  let token = {
    'access-token': useUserStore().row.tokenValue,
  }
  return request({
    url: '/system/login/getCustomerCode',
    method: 'get',
    params,
    headers: token,
  })
}
// 注册企业
export function registerCustomer(data) {
  let token = {
    'access-token': useUserStore().row.tokenValue,
  }
  return request({
    url: '/system/customer/register',
    method: 'post',
    data,
    headers: token,
  })
}
// 刷新token
export function refreshToken(params) {
  let token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/system/login/refreshToken',
    method: 'get',
    headers: token,
    params,
  })
}
// 获取登录账号信息
export function getInfoUser() {
  let token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/system/account/getInfo',
    method: 'get',
    headers: token,
  })
}
// 修改个人信息
export function updateInfo(data) {
  let token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/system/account/updateInfo',
    method: 'PUT',
    data,
    headers: token,
  })
}
// 修改密码
export function updatePassword(data) {
  let token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/system/account/updatePassword',
    method: 'PUT',
    data,
    headers: token,
  })
}

/**
 * 获取更改密码的验证码
 * @param phone - 手机号
 */
export const getUpdatePwdVerifyCode = (phone: string): AxiosPromise<APIResponse> => {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/sms/updateCode',
    method: 'get',
    params: {
      phone,
    },
    headers: token,
  })
}
